<!--职位类别-->
<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item class="form-button-right">
				<el-button v-auth="'module:position/type:save'" type="primary" @click="addOrUpdateHandle()">新增职级</el-button>
				<el-button v-auth="'module:position/type:delete'" type="danger"
					@click="deleteBatchHandle()">批量删除</el-button>
				<el-button type="danger" @click="toggleExpandAll()">
					<el-icon style="width: 100%">
						<Sort />展开/折叠
					</el-icon>
				</el-button>
			</el-form-item>

			<el-form-item>
				<KeyQuery v-model="state.queryForm.keyword" @getDataList="getDataList()" />
			</el-form-item>
		</el-form>

		<el-table v-if="refreshTable" v-loading="state.dataListLoading" :default-expand-all="isExpandAll"
			:data="state.dataList" row-key="id" border style="width: 100%">
			<el-table-column type="selection" header-align="center" align="center" width="50"
				v-if="false"></el-table-column>

			<el-table-column width="200" prop="name" label="组织名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="description" label="描述" header-align="center" align="center"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="250">
				<template #default="scope">
					<el-button v-auth="'module:organization:update'" type="primary" link
						@click="addSubset(scope.row.id)">添加下级</el-button>
					<el-button v-auth="'module:position/type:update'" type="primary" link
						@click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
					<el-button v-auth="'module:position/type:delete'" type="primary" link
						@click="deleteBatchHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="ModulePositionTypeIndex">
import { useCrud } from '@/hooks'
import { reactive, ref, nextTick } from 'vue'
import { IHooksOptions } from '@/hooks/interface'

import { Sort } from '@element-plus/icons-vue'

import AddOrUpdate from './plane/type-a-u.vue'

const state: IHooksOptions = reactive({
	dataListUrl: '/nb/position_type/list',
	deleteUrl: '/nb/position_type/delete',
	queryForm: {},
	isPage: false
})

//新增页面
const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.init(id)
}

//添加下级页面
const addSubset = (id?: number) => {
	addOrUpdateRef.value.pid = id
	addOrUpdateRef.value.init()
}

// 是否展开，默认全部折叠
const isExpandAll = ref(true)
// 是否重新渲染表格状态
const refreshTable = ref(true)

/**
 * 切换 展开/折叠
 */
const toggleExpandAll = () => {
	refreshTable.value = false
	isExpandAll.value = !isExpandAll.value
	nextTick(() => {
		refreshTable.value = true
	})
}

const { getDataList, deleteBatchHandle } = useCrud(state)
</script>
